﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

public partial class CreateNewApplicant : System.Web.UI.Page
{
    ApplicantMethord App = new ApplicantMethord();
    protected void Page_Load(object sender, EventArgs e)
    {
        txtUserName.Focus();
        txtDateBirth_CalendarExtender.EndDate = DateTime.Now;
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        txtUserName.Text = "";
        txtPassword.Text = "";
        txtFullName.Text = "";
        txtAddress.Text = "";
        txtPhone.Text = "";
        txtDateBirth.Text = "";
        txtEmail.Text = "";
        lblErrorAddress.Text = "";
        lblErrorDateBirth.Text = "";
        lblErrorEmail.Text = "";
        lblErrorName.Text = "";
        lblErrorPass.Text = "";
        lblErrorPhone.Text = "";
        lblErrorUser.Text = "";
    }
    protected void btnCreate_Click(object sender, EventArgs e)
    {
        lblError.Text = "";
        string mathEmail = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
        string mathPhone = @"^[\+\-]?\d*\.?[Ee]?[\+\-]?\d*$";
        if (txtUserName.Text.Equals(""))
        {
            lblErrorUser.Text = "*";
        }
        else
        {
            lblErrorUser.Text = "";
            if (txtPassword.Text.Equals(""))
            {
                lblErrorPass.Text = "(*)";
            }
            else
            {
                lblErrorPass.Text = "";
                if (txtFullName.Text.Equals(""))
                {
                    lblErrorName.Text = "(*)";
                }
                else
                {
                    lblErrorName.Text = "";
                    if (txtAddress.Text.Equals(""))
                    {
                        lblErrorAddress.Text = "(*)";
                    }
                    else
                    {
                        lblErrorAddress.Text = "";
                        if ((txtPhone.Text.Equals("")) || (Regex.IsMatch(txtPhone.Text, mathPhone)) == false)
                        {
                            lblErrorPhone.Text = "(*)";
                        }
                        else
                        {
                            lblErrorPhone.Text = "";
                            if (txtDateBirth.Text.Equals(""))
                            {
                                lblErrorDateBirth.Text = "(*)";
                            }
                            else
                            {
                                lblErrorDateBirth.Text = "";
                                if ((txtEmail.Text.Equals("")) || (Regex.IsMatch(txtEmail.Text, mathEmail)) == false)
                                {
                                    lblErrorEmail.Text = "(*)";
                                }
                                else
                                {
                                    lblErrorEmail.Text = "";
                                    if (App.CheckEmail(txtEmail.Text) == false)
                                    {
                                        lblError.Text = "This Email exist!";
                                    }
                                    else
                                    {
                                        bool Result = App.CreateNewApplicant(txtUserName.Text, txtPassword.Text, txtFullName.Text, txtAddress.Text, txtPhone.Text, txtDateBirth.Text, txtEmail.Text);
                                        if (Result)
                                        {
                                            Response.Write("<script type='text/javascript'>alert('Create new Applicant Successful!'); window.location = 'LoginApplicant.aspx'</script>");
                                        }
                                        else
                                        {
                                            lblError.Text = "UserName already exist.";
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}